

package com.mt.mall.controller.good;

import io.lettuce.core.dynamic.annotation.Param;
import org.springframework.security.access.prepost.PreAuthorize;
import com.mt.common.core.annotation.ApiPageParam;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import com.mt.common.core.web.base.PageDTO;
import com.mt.common.core.web.base.PageResultDTO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;


import com.mt.mall.dto.good.SpecificationStockEditDto;
import com.mt.mall.entity.good.SpecificationStock;
import com.mt.mall.service.good.SpecificationStockService;
import com.mt.mall.service.good.CommodityService;


@Api(tags = "商品销售规格")
@RestController
@RequestMapping("/api/mall/good/SpecificationStock")
@CrossOrigin(allowCredentials = "true")
public class SpecificationStockController {
    private static Logger logger = LoggerFactory.getLogger(SpecificationStockController.class);


    @Autowired
    private SpecificationStockService specificationStockService;
    @Autowired
    private CommodityService commodityService;

    /**
     * 根据分页参数查询商品销售规格集合
     *
     * @param pageDTO 分页条件
     */
    @PreAuthorize("hasAuthority('mall:good:SpecificationStock:view')")
    @ApiOperation("分页查询商品销售规格")
    @ApiPageParam
    @PostMapping("/findSpecificationStocks")
    public PageResultDTO findSpecificationStocks(@RequestBody PageDTO pageDTO) {
        return this.specificationStockService.findSpecificationStocks(pageDTO);
    }

    /**
     * 根据ID查询指定的商品销售规格
     *
     * @param specificationStockId Id
     */
    @PreAuthorize("hasAuthority('mall:good:SpecificationStock:view')")
    @ApiOperation("根据id查询商品销售规格")
    @ApiPageParam
    @PostMapping("/findSpecificationStock")
    public SpecificationStock findSpecificationStock(@RequestParam Long specificationStockId) {
        return this.specificationStockService.findSpecificationStock(specificationStockId);
    }

    @ApiOperation("根据商品id查询商品销售规格")
    @ApiPageParam
    @PostMapping("/findSpecificationStockByCommodityId")
    public List<SpecificationStock> findSpecificationStockByCommodityId(@Param("commodityId") Long commodityId) {
        return this.specificationStockService.findSpecificationStockByCommodityId(commodityId);
    }

    /**
     * 根据ID查询指定的商品销售规格(包含外键名称)
     *
     * @param specificationStockId Id
     */
    @PreAuthorize("hasAuthority('mall:good:SpecificationStock:view')")
    @ApiOperation("根据ID查询指定的商品销售规格(包含外键名称)")
    @PostMapping("/findSpecificationStockForView")
    public SpecificationStock findSpecificationStockForView(@RequestParam Long specificationStockId) {
        return this.specificationStockService.findSpecificationStockWithForeignName(specificationStockId);
    }

    /**
     * 根据ID查询指定的商品销售规格(包含商品销售规格和外键名称)
     *
     * @param specificationStockId Id
     */
    @PreAuthorize("hasAuthority('mall:good:SpecificationStock:view')")
    @ApiOperation("根据ID查询指定的商品销售规格(包含商品销售规格和外键名称)")
    @PostMapping("/findSpecificationStockForEdit")
    public SpecificationStockEditDto findSpecificationStockForEdit(@RequestParam Long specificationStockId) {
        SpecificationStockEditDto specificationStockEditDto = new SpecificationStockEditDto();
        specificationStockEditDto.setSpecificationStock(this.specificationStockService.findSpecificationStockWithForeignName(specificationStockId));

        this.prepareSpecificationStockEditDto(specificationStockEditDto);

        return specificationStockEditDto;
    }

    /**
     * 根据ID查询指定的商品销售规格(只提取ID 和 Name)
     *
     * @param specificationStockId Id
     */
    @PreAuthorize("hasAuthority('mall:good:SpecificationStock:view')")
    @ApiOperation("根据ID查询指定的商品销售规格(只提取ID 和 Name)")
    @PostMapping("/findSpecificationStocksWithIdNameById")
    public SpecificationStock findSpecificationStocksWithIdNameById(@RequestParam Long specificationStockId) {
        return this.specificationStockService.findSpecificationStocksWithIdNameById(specificationStockId);
    }

    /**
     * 根据名称查询商品销售规格集合(只提取ID 和 Name)
     *
     * @param specificationStockName 名称
     */
    @PreAuthorize("hasAuthority('mall:good:SpecificationStock:view')")
    @ApiOperation("根据名称查询商品销售规格集合(只提取ID 和 Name)")
    @PostMapping("/findSpecificationStocksWithIdNameByName")
    public List<SpecificationStock> findSpecificationStocksWithIdNameByName(@RequestParam String specificationStockName) {
        return this.specificationStockService.findSpecificationStocksWithIdNameByName(specificationStockName);
    }


    /**
     * 创建新的商品销售规格
     */
    @PreAuthorize("hasAuthority('mall:good:SpecificationStock:add')")
    @ApiOperation("创建新的商品销售规格")
    @PostMapping("/createSpecificationStock")
    public SpecificationStockEditDto createSpecificationStock() {
        SpecificationStockEditDto specificationStockEditDto = new SpecificationStockEditDto();
        specificationStockEditDto.setSpecificationStock(new SpecificationStock());

        this.prepareSpecificationStockEditDto(specificationStockEditDto);
        return specificationStockEditDto;
    }

    private void prepareSpecificationStockEditDto(SpecificationStockEditDto specificationStockEditDto) {
        //TODO: 以下代码可以注释掉，此行代码即时加载所有外键对象，以便选择。如果不在此加载，可以在界面上实现延迟加载。如果外键对象超过 500 行，建议采用延迟加载
        specificationStockEditDto.setCommodityCommoditys(this.commodityService.findAllCommoditysWithIdName());
    }

    /**
     * 新增保存商品销售规格
     *
     * @param specificationStock 实体对象
     */
    @PreAuthorize("hasAuthority('mall:good:SpecificationStock:add')")
    @ApiOperation("新增保存商品销售规格")
    @PostMapping("/saveSpecificationStock")
    public SpecificationStock saveSpecificationStock(@RequestBody SpecificationStock specificationStock) {
        return this.specificationStockService.saveSpecificationStock(specificationStock);
    }

    /**
     * 修改保存商品销售规格
     *
     * @param specificationStock 实体对象
     */
    @PreAuthorize("hasAuthority('mall:good:SpecificationStock:update')")
    @ApiOperation("修改保存商品销售规格")
    @PostMapping("/updateSpecificationStock")
    public SpecificationStock updateSpecificationStock(@RequestBody SpecificationStock specificationStock) {
        return this.specificationStockService.updateSpecificationStock(specificationStock);
    }

    /**
     * 根据ID删除商品销售规格
     *
     * @param specificationStockId ID
     */
    @PreAuthorize("hasAuthority('mall:good:SpecificationStock:remove')")
    @ApiOperation("根据ID删除商品销售规格")
    @PostMapping("/deleteSpecificationStock")
    public void deleteSpecificationStock(@RequestParam Long specificationStockId) {
        this.specificationStockService.deleteSpecificationStock(specificationStockId);
    }

}

